package com.sky.computer;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.graphics.Paint;

public class ComputerActivity extends Activity {

	private TextView text1;
	private TextView text2;
	
	private int width;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_computer);
        DisplayMetrics  dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screen_width = dm.widthPixels;
        float scale = getResources().getDisplayMetrics().density;
        
        width = screen_width - (int)(30 * scale + 0.5f) - 1;
        
        text1 = (TextView)findViewById(R.id.textView1);
        text2 = (TextView)findViewById(R.id.textView2);
        
        text1.setText("");
        text2.setText("0");
        
        TableRow row1 = (TableRow)findViewById(R.id.tableRow1);
        Button button1 = (Button)row1.findViewById(R.id.button1);
        button1.setText("DEL");
        button1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String tmp = text2.getText().toString();
				if (tmp.length() > 1) {
					text2.setText(tmp.substring(0, tmp.length() - 1));
				} else {
					text2.setText("0");
				}
				
			}
		});
        Button button2 = (Button)row1.findViewById(R.id.button2);
        button2.setText("CE	");
        button2.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		text2.setText("0");
        		
        	}
        });
        Button button3 = (Button)row1.findViewById(R.id.button3);
        button3.setText("	C	");
        button3.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		text1.setText("");
        		text2.setText("0");
        	}
        });
        Button button4 = (Button)row1.findViewById(R.id.button4);
        button4.setText("	±	");
        button4.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
        		if (tmp.indexOf(".") != -1) {
        			if (tmp.startsWith("-")) {
						text2.setText(tmp.substring(1));
					} else {
						text2.setText("-" + tmp);
					}
        		} else {
        			float f = Float.valueOf(tmp);
        			if (f != 0.0) {
        				if (tmp.startsWith("-")) {
        					text2.setText(tmp.substring(1));
						} else {
							text2.setText("-" + tmp);
						}
					}
        		}
        	}
        });
        
        
        TableRow row2 = (TableRow)findViewById(R.id.tableRow2);
        Button button5 = (Button)row2.findViewById(R.id.button1);
        button5.setText("7");
        button5.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "7")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "7");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("7");
						} else {
							text2.setText(text2.getText().toString() + "7");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
			}
		});
        Button button6 = (Button)row2.findViewById(R.id.button2);
        button6.setText("8");
        button6.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "8")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "8");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("8");
						} else {
							text2.setText(text2.getText().toString() + "8");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button7 = (Button)row2.findViewById(R.id.button3);
        button7.setText("9");
        button7.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "9")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "9");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("9");
						} else {
							text2.setText(text2.getText().toString() + "9");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button8 = (Button)row2.findViewById(R.id.button4);
        button8.setText("+");
        button8.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		Log.i("sky", "click");
        		
        	}
        });
        
        
        TableRow row3 = (TableRow)findViewById(R.id.tableRow3);
        Button button9 = (Button)row3.findViewById(R.id.button1);
        button9.setText("4");
        button9.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "4")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "4");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("4");
						} else {
							text2.setText(text2.getText().toString() + "4");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
			}
		});
        Button button10 = (Button)row3.findViewById(R.id.button2);
        button10.setText("5");
        button10.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "5")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "5");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("5");
						} else {
							text2.setText(text2.getText().toString() + "5");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button11 = (Button)row3.findViewById(R.id.button3);
        button11.setText("6");
        button11.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "6")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "6");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("6");
						} else {
							text2.setText(text2.getText().toString() + "6");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button12 = (Button)row3.findViewById(R.id.button4);
        button12.setText("-");
        button12.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		Log.i("sky", "click");
        		
        	}
        });
        
        TableRow row4 = (TableRow)findViewById(R.id.tableRow4);
        Button button13 = (Button)row4.findViewById(R.id.button1);
        button13.setText("1");
        button13.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "8")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "1");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("1");
						} else {
							text2.setText(text2.getText().toString() + "1");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
			}
		});
        Button button14 = (Button)row4.findViewById(R.id.button2);
        button14.setText("2");
        button14.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "8")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "2");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("2");
						} else {
							text2.setText(text2.getText().toString() + "2");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button15 = (Button)row4.findViewById(R.id.button3);
        button15.setText("3");
        button15.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		String tmp = text2.getText().toString();
				if (canWrite(tmp, text2, "8")) {
					if (tmp.indexOf(".") != -1) {
						text2.setText(text2.getText().toString() + "3");
					} else {
						float f = Float.valueOf(tmp);
						if (f == 0.0) {
							text2.setText("3");
						} else {
							text2.setText(text2.getText().toString() + "3");
						}
					}
				} else {
					Toast.makeText(ComputerActivity.this, R.string.text_long, Toast.LENGTH_SHORT).show();
				}
        	}
        });
        Button button16 = (Button)row4.findViewById(R.id.button4);
        button16.setText("*");
        button16.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		Log.i("sky", "click");
        		
        	}
        });
        
        TableRow row5 = (TableRow)findViewById(R.id.tableRow5);
        Button button17 = (Button)row5.findViewById(R.id.button1);
        button17.setText("0");
        button17.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String tmp = text2.getText().toString();
				if (tmp.indexOf(".") != -1) {
					text2.setText(text2.getText().toString() + "0");
				} else {
					float f = Float.valueOf(tmp);
					if (f == 0.0) {
						text2.setText("0");
					} else {
						text2.setText(text2.getText().toString() + "0");
					}
				}
			}
		});
        Button button18 = (Button)row5.findViewById(R.id.button2);
        button18.setText(".");
        button18.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		text2.setText(text2.getText().toString() + ".");
        	}
        });
        Button button19 = (Button)row5.findViewById(R.id.button3);
        button19.setText("=");
        button19.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		Log.i("sky", "click");
        		
        	}
        });
        Button button20 = (Button)row5.findViewById(R.id.button4);
        button20.setText("/");
        button20.setOnClickListener(new View.OnClickListener() {
        	
        	@Override
        	public void onClick(View v) {
        		Log.i("sky", "click");
        		
        	}
        });
    }

    private boolean canWrite(String text, TextView view, String num) {
    	Paint paint = new Paint();
    	paint.setTextSize(view.getTextSize());
    	float size = paint.measureText(text);
    	float size2 = paint.measureText(num);
    	return width - size - size2 > 0;
    }
}
